package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LevelServiceImpl extends ServiceImpl<LevelMapper, LevelDO> implements LevelService {

    @Autowired
    LevelConverter levelConverter;

    @Override
    public void addLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        this.save(levelDO);
    }

    @Override
    public void updateLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        this.updateById(levelDO);
    }

    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        // 构造分页对象，参数为当前页码和每页条数
        Page<LevelDO> page = new Page<>(request.getPageNum(), request.getCount());

        // 构造查询条件，如果有关键词则按等级名称模糊查询
        LambdaQueryWrapper<LevelDO> wrapper = new LambdaQueryWrapper<>();
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            wrapper.like(LevelDO::getLevelName, request.getKeyword());
        }

        // 执行分页查询
        Page<LevelDO> levelPage = this.page(page, wrapper);

        // 将DO列表转换为DTO列表
        List<LevelDTO> dtoList = levelPage.getRecords().stream()
                .map(levelConverter::levelDOToLevelDTO)
                .toList();

        // 封装分页结果
        // Page<LevelDO> 是数据库分页查询的载体，只查当前页数据，自动统计总数。
        // PageDTO 是自定义的返回结构，用于前后端数据交互。
        PageDTO<LevelDTO> result = new PageDTO<>();
        result.setPageList(dtoList);
        result.setTotalCount(levelPage.getTotal());
        return result;
    }

    @Override
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        // 先查出要删除的等级信息（用于操作日志）
        LevelDO levelDO = this.getById(id);
        if (levelDO == null) {
            return null;
        }
        // 逻辑删除
        this.removeById(id);

        // 封装删除操作日志DTO
        DeleteLevelOperateDTO dto = new DeleteLevelOperateDTO();
        dto.setId(levelDO.getId());
        dto.setLevelName(levelDO.getLevelName());
        return dto;
    }


    @Override
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        // 将字符串id集合转为Long集合
        List<Long> idList = command.getIds().stream()
                .map(str -> Long.valueOf(str)).toList();

        // 查询所有要删除的等级信息
        List<LevelDO> levelList = this.listByIds(idList);

        // 逻辑删除
        this.removeByIds(idList);

        // 封装每个被删等级的操作日志DTO
        return levelList.stream().map(levelDO -> {
            DeleteLevelOperateDTO dto = new DeleteLevelOperateDTO();
            dto.setId(levelDO.getId());
            dto.setLevelName(levelDO.getLevelName());
            return dto;
        }).toList();
    }

}
